package com.quanyan.place.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqVisitedVo;
import com.quanyan.place.entity.apiresp.RespVisitInfo;
import com.quanyan.place.entity.vo.VisitedFromDataVo;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by liuyaoxin on 2016/6/16
 */
@Service("visdService")
public class VisitedServiceImpl implements VisitedService {
    private static final Logger logger = LoggerFactory.getLogger(VisitedServiceImpl.class);


    @Autowired
    TbBizPlaceMapper placeMapper;

    @Autowired
    VisitedMapper visitedMapper;
    @Autowired
    TbBizPlaceEmployeeMapper tbBizPlaceEmployeeMapper;

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    PlaceInnerService placeInnerService;
    @Autowired
    TbBizPlaceVisitedMapper tbBizPlaceVisitedMapper;


    @Override
    public APIResponse importVisitedRecord(VisitedFromDataVo visitedFormDataVo) {
        //被拜访人手机为空表示是场馆录入时记录的拜访日志
        if (visitedFormDataVo.getBeVisitedPhone() != null) {
            //拜访时间不能大于当前时间
            if (this.isVisitedTimeMax(visitedFormDataVo)) {
                return APIResponse.returnFail("拜访时间不能大于当前时间");
            }
        }
        visitedMapper.insert(visitedFormDataVo);
        return APIResponse.returnSuccess();
    }

    /**
     * 录入时间不能大于当前时间
     * @param visitedFormDataVo
     * @return
     */
    public boolean isVisitedTimeMax(VisitedFromDataVo visitedFormDataVo) {
            Date currentTime = new Date();
            boolean flag = false;
            if(visitedFormDataVo.getVisitedTime()!=null){
            flag=DateUtils.dateCompare(DateUtils.dateStrToDate(visitedFormDataVo.getVisitedTime()),currentTime,false);
                return flag;
            }else{
                return flag;
            }
    }


    /*
    获得当前职称
     */
    public TbBizPlaceEmployee getTitleRoleByUid(VisitedFromDataVo vo) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(vo.getUid());
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        if (null != list && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }


    @Override
    public APIResponse updateVisitedRecord(VisitedFromDataVo visitedFromDataVo) {
        TbBizPlaceEmployee employee = this.getTitleRoleByUid(visitedFromDataVo);
        if (employee != null) {
            if (employee.getTitle() == 5) {
                visitedMapper.updateVisitedRecord(visitedFromDataVo);
            } else if (employee.getTitle() == 4) {
                visitedMapper.updateVisitedRecordByLeader(visitedFromDataVo);
            } else {
                return APIResponse.returnFail("修改失败");
            }
        }
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse selectVisitedRecord(ReqVisitedVo reqVisitedVo) {
        TbBizPlaceVisitedExample visitedExample=new TbBizPlaceVisitedExample();
        TbBizPlaceVisitedExample.Criteria criteria=visitedExample.createCriteria();
        criteria.andIdEqualTo(reqVisitedVo.getId());
       List<TbBizPlaceVisited> listVo=tbBizPlaceVisitedMapper.selectByExample(visitedExample);
        if (null != listVo && listVo.size()>0){
            TbBizPlaceVisited vo=listVo.get(0);
            TbBizPlace place = placeMapper.selectByPrimaryKey(vo.getPlaceId());
            RespVisitInfo respVisitInfo=new RespVisitInfo();
            respVisitInfo.setResult(vo.getResult());
            respVisitInfo.setBussinessRepresent(vo.getBussinessRepresent());
            respVisitInfo.setCityId(vo.getCityId());
            if(place!=null) {
                respVisitInfo.setName(place.getName());
            }
            respVisitInfo.setBeVisitedDuites(vo.getBeVisitedDuites());
            respVisitInfo.setId(vo.getId());
            respVisitInfo.setPlaceId(vo.getPlaceId());
            respVisitInfo.setBeVisitedPeople(vo.getBeVisitedPeople());
            respVisitInfo.setBeVisitedPhone(vo.getBeVisitedPhone());
            respVisitInfo.setPicture(vo.getPicture());
            respVisitInfo.setBuyWay(vo.getBuyWay());
            respVisitInfo.setPrice(vo.getPrice());
            respVisitInfo.setRemark(vo.getRemark());
            respVisitInfo.setLeaderOpinion(vo.getLeaderOpinion());
            respVisitInfo.setVisitedTime(vo.getVisiteTime());
            return APIResponse.returnSuccess(respVisitInfo);
        }else{
            return APIResponse.returnSuccess(null);
        }
    }


    @Override
    public APIResponse<PageObj<List<RespVisitInfo>>> selectVisitedRecordList(ReqVisitedVo reqVisitedVo) {


        Integer title = employeeService.getEmployeeTitleByUid(reqVisitedVo.getUid());
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        int total=0;
        if (StringUtils.isEmpty(reqVisitedVo.getName())) {
            reqVisitedVo.setName(null);
        }
        if (StringUtils.isEmpty(reqVisitedVo.getBussinessRepresent())) {
            reqVisitedVo.setBussinessRepresent(null);
        }
        if (!StringUtils.isEmpty(reqVisitedVo.getName())) {
            reqVisitedVo.setName("%" + reqVisitedVo.getName() + "%");
        }
        if (!StringUtils.isEmpty(reqVisitedVo.getBussinessRepresent())) {
            reqVisitedVo.setBussinessRepresent("%" + reqVisitedVo.getBussinessRepresent() + "%");
        }

        pageNum = reqVisitedVo.getPageNum();
        pageSize = reqVisitedVo.getPageSize();
        if(title!=null) {
            if (PlaceConstants.TITLE_BUSSINESS_REPRESENT.equals(title)) {
                total = visitedMapper.getVisitedInfoToRePCount(reqVisitedVo);
                reqVisitedVo.setPage(new Page(pageNum * pageSize, pageSize, total));
                List<RespVisitInfo> list = visitedMapper.getVisitedInfoToReP(reqVisitedVo);
                PageObj<List<RespVisitInfo>> result = PageObj.create(total, pageNum, pageSize, list);
                return APIResponse.returnSuccess(result);
            } else if (PlaceConstants.TITLE_BUSSINESS_DIRECTOR.equals(title)) {
                total = visitedMapper.getVisitedInfoCount(reqVisitedVo);
                reqVisitedVo.setPage(new Page(pageNum * pageSize, pageSize, total));
                List<RespVisitInfo> list = visitedMapper.getVisitedInfo(reqVisitedVo);
                PageObj<List<RespVisitInfo>> result = PageObj.create(total, pageNum, pageSize, list);
                return APIResponse.returnSuccess(result);
            }
        }
            return APIResponse.returnSuccess();
    }




       public void importVisitedRecordByImportPlace(TbBizPlace tbBizPlace){
            Date d =new Date();
            TbAdminUser user = adminUserService.getAdminUser(tbBizPlace.getUid());
            //插入拜访记录
           for (int i=0;i<2;i++) {
               String timeStr = DateUtils.transferDateToString(d);
               VisitedFromDataVo visitedFormDataVo = new VisitedFromDataVo();
               visitedFormDataVo.setPlaceId(tbBizPlace.getId());
               visitedFormDataVo.setName(tbBizPlace.getName());
               visitedFormDataVo.setBussinessRepresent(user.getTrueName());
               if (i==0) {
                   visitedFormDataVo.setResult(5);
               }else{
                   visitedFormDataVo.setResult(2);
               }
               visitedFormDataVo.setVisitedTime(timeStr);
               this.importVisitedRecord(visitedFormDataVo);
           }
            }
}
